package com.taplinker.client.crm.rpc;

import android.content.Context;
import android.widget.Toast;

import com.google.protobuf.InvalidProtocolBufferException;
import com.taplinker.client.AppConfigContext;
import com.taplinker.client.crm.domain.CrmDataCenter;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.dto.web.CustomerProto.Customer_V2;

/**
 * 获取用户ID
 * 
 * @author djbian
 * 
 */
public class GetCustomerById extends SimpleBaseCommand {
	private String customerId;

	public GetCustomerById(Context ctx, String customerId) {
		context = ctx;
		this.customerId = customerId;
	}

	@Override
	public void yes(Result result) {
		super.yes(result);
		try {
			CrmDataCenter.getInstance().setCustomer(
					Customer_V2.parseFrom((byte[]) result.object));
		} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
		}
		CrmDataCenter.getInstance().noticeListener(CrmDataCenter.GET_CUSTOMER);
	}

	@Override
	public void no(Result result) {
		Toast.makeText(context, "" + result.message, Toast.LENGTH_SHORT).show();
	}

	@Override
	public Result go() {
		return new HttpTemplate().exchange(AppConfigContext.TAPLINKER_WEB
				+ "/services/crm/v2/info/customer/" + customerId);
	}
}